package com.pb.poem.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import com.pb.poem.dao.PoemDao;
import com.pb.poem.dao.impl.PoemDaoImpl;
import com.pb.poem.entity.Common;
import com.pb.poem.entity.Poem;
import com.pb.poem.service.impl.PoemServiceImpl;
import com.pb.poem.util.Page;

public class AllPoem extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public AllPoem() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// HttpServletResponse res = ServletActionContext.getResponse();
		// response.reset();
		PrintWriter pw = response.getWriter();
		// request.setCharacterEncoding("utf-8");/////////////////////////
		PoemServiceImpl poemService = new PoemServiceImpl();
		PoemDao poemDao = new PoemDaoImpl();
		poemService.setPoemDao(poemDao);

		// String currentPage=request.getParameter("pageIndex");
		// System.out.println("页码："+currentPage);
		// if(currentPage==null)
		// currentPage="1";
		//
		// int pageIndex=Integer.parseInt(currentPage);
		//
		//
		// if(pageIndex<1){
		// pageIndex=1;
		// }
		// else if(pageIndex>totalPage){
		// pageIndex=totalPage;
		// }

		int id = Integer.parseInt(request.getParameter("pId"));
		//System.out.println("按id查询全部，起始id=" + id);// ////////////////////////////
		// 每页显示的诗列表
		List<Poem> list = poemService.getPagePoemList(id);
		//System.out.print("诗的id：");// //////////////////
		for (Poem poem : list) {
			String p = poem.getContent();
			p = p.replaceAll("，", "，<br />");
			p = p.replaceAll("。", "。<br />");
			poem.setContent(p);
			//System.out.print(poem.getId() + ",");// ////////////
		}
		//System.out.println();// ///////////////////////
		JSONArray ja = new JSONArray(list);
		String json = ja.toString();

		pw.print(json);
		pw.flush();
		pw.close();

	}

}